package com.tianxin.workbao.workBao.dataProvide;

import android.content.Context;
import android.util.Log;

import com.tianxin.workbao.workBao.common.Constant;
import com.tianxin.workbao.workBao.helper.GreenDaoHelper;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;
import com.tianxin.workbao.workBao.okhttp.listener.DisposeDataHandle;
import com.tianxin.workbao.workBao.okhttp.listener.DisposeDataListener;
import com.tianxin.workbao.workBao.utils.common.SPUtils;

import java.text.SimpleDateFormat;

/**
 * Created by hacker on 2017/6/18.
 */

public abstract class BaseDataProvide {
    public String TAG = getClass().getSimpleName() + "------>";
    public GreenDaoHelper mGreenDaoHelper;
    public Context mContext;
    public DisposeDataHandle disposeDataHandle;
    public DisposeDataListener disposeDataListener;

    public static SimpleDateFormat dateYearFormat = new SimpleDateFormat("yyyy");
    public static SimpleDateFormat dateMonthFormat = new SimpleDateFormat("yyyy-MM");
    public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    public static SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");

    public static final int TODAY_DATA = 0;
    public static final int WEEK_DATA = 1;
    public static final int MONTH_DATA = 2;
    public static final int SEASON_DATA = 3;
    public static final int YEAR_DATA = 4;

    public BaseDataProvide(Context context) {
        this.mGreenDaoHelper = GreenDaoHelper.newInstance();
        this.mContext = context;
        initGreenDAO();
    }

    private void initGreenDAO() {
        mGreenDaoHelper = GreenDaoHelper.newInstance();
        mGreenDaoHelper.init(mContext);
        mGreenDaoHelper.setDebug(Constant.GREEN_DAO_DEBUG);
    }

    public abstract DisposeDataHandle getDisposeDataHandle();

    public abstract DisposeDataListener getDisposeDataListener();

    public abstract void refreshUI();

    public String getAppToken() {
        return SPUtils.getPrefString(Constant.CURRENT_USER_APPTOKEN, "apptoken");
    }

    /**
     * show request response error message
     *
     * @param listener
     * @param response
     */
    public void showRequestErrorMessage(UpdateListViewListener listener, String response) {
        String errorMsg = "";
        if (response.contains("<h1>")) {
            errorMsg = response.substring(response.indexOf("<h1>") + 1, response.indexOf("</h1>"));
        }
        if (errorMsg.contains("404")) {
            listener.showMessage("请求的地址出错或者服务器死机");
        } else if (errorMsg.contains("400")) {
            listener.showMessage("请求的参数出错");
        } else if (errorMsg.contains("500")) {
            listener.showMessage("服务器代码运行出错");
        } else {
            listener.showMessage("返回异常:不能转化为JsonObject " + response);
        }
    }


}
